﻿namespace tomenglertde.SetupProject.View
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Input;

    using tomenglertde.SetupProject.ViewModel;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class ProjectView
    {
        public ProjectView()
        {
            InitializeComponent();
        }

        public SolutionViewModel Solution
        {
            get
            {
                return (SolutionViewModel)DataContext;
            }
        }

        public SetupProjectViewModel Project
        {
            get { return (SetupProjectViewModel)GetValue(ProjectProperty); }
            set { SetValue(ProjectProperty, value); }
        }
        /// <summary>
        /// Identifies the Project dependency property
        /// </summary>
        public static readonly DependencyProperty ProjectProperty =
            DependencyProperty.Register("Project", typeof(SetupProjectViewModel), typeof(ProjectView), new UIPropertyMetadata(null, (sender, e) => ((ProjectView)sender).Project_Changed((SetupProjectViewModel)e.NewValue)));

        private void Project_Changed(SetupProjectViewModel newValue)
        {
            if (newValue == null)
            {
                ProjectMappings = Enumerable.Empty<ProjectMapping>();
            }
            else
            {
                ProjectMappings = Solution.Projects.Select(p => new ProjectMapping(newValue, p))
                    .Where(p => !string.IsNullOrEmpty(p.Guid))
                    .ToArray();
            }
        }

        public IEnumerable<ProjectMapping> ProjectMappings
        {
            get { return (IEnumerable<ProjectMapping>)GetValue(SolutionProjectsProperty); }
            set { SetValue(SolutionProjectsProperty, value); }
        }
        /// <summary>
        /// Identifies the ProjectMappings dependency property
        /// </summary>
        public static readonly DependencyProperty SolutionProjectsProperty =
            DependencyProperty.Register("ProjectMappings", typeof(IEnumerable<ProjectMapping>), typeof(ProjectView));


        private void DataGridProjects_AutoGeneratedColumns(object sender, System.EventArgs e)
        {
            var dataGrid = (DataGrid)sender;
            var columns = dataGrid.Columns;

            columns.Where(c => !c.IsAutoGenerated).ToList().ForEach(c => columns.Remove(c));

            var outputNames = ProjectMappings.SelectMany(p => p.OutputNames).Distinct();

            foreach (var outputName in outputNames)
            {
                var binding = new Binding("[" + outputName + "]") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };

                var column = new DataGridCheckBoxColumn()
                {
                    Header = outputName,
                    Binding = binding,
                    IsThreeState = true,
                };

                columns.Add(column);
            }
        }

        private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if ((e.PropertyType != typeof(string)) && typeof(IEnumerable).IsAssignableFrom(e.PropertyType))
            {
                e.Cancel = true;
            }
        }

        private void DataGridFile_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Delete:
                    DeleteSelected((DataGrid)sender);
                    break;
            }
        }

        private void DataGridFile_DeleteClick(object sender, RoutedEventArgs e)
        {
            DeleteSelected(DataGridFile);
        }

        private static void DeleteSelected(MultiSelector dataGrid)
        {
            var selectedItems = dataGrid.SelectedItems.OfType<SetupItem>().ToArray();

            foreach (var item in selectedItems)
            {
                item.Remove();
            }
        }
    }
}
